//对象方法
const user = {
  name: "张三",
  age: "20",
  //定义
  printName: function () {
    console.log(this.name);
  },
  printAge() {
    console.log(this.age);
  },
};
user.printInfo = function () {
  //console.log("姓名是: " + this.name + "; 年龄是: " + this.age);
  console.log(`姓名是: ${this.name}; 年龄是: ${this.age}`);
};
//方法
user.printName();
user.printAge();
user.printInfo();

//构造函数
function Dog(name, age) {
  (this.name = name), (this.age = age);
}
//调用
const dog1 = new Dog("大黄", 2);
console.log(dog1);
const dog2 = Dog("大黄", 2);
console.log(dog2); //undefined

//尝试读取未定义属性 other
//console.log(dog1.other.color);
//可选链
console.log(dog1.other);
console.log(dog1.other?.color);
dog1.method?.();
